﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Omnia.DataModels;
using Omnia.Enums;
using Omnia.Library;

namespace Omnia.ViewModels
{
    public class CardsViewModel : MainViewModel
    {
        #region Collections
        public ObservableCollection<CardInfo> CreditCardsCollection
        {
            get
            {
                return new ObservableCollection<CardInfo>(CardsCollection.Where(c => c.Kind == CardKindValue.Credit).OrderBy(c => c.NextRepaymentDate));
            }
        }

        public ObservableCollection<CardInfo> DebitCardsCollection
        {
            get
            {
                return new ObservableCollection<CardInfo>(CardsCollection.Where(c => c.Kind == CardKindValue.Debit));
            }
        } 
        #endregion

        #region Models
        private CardInfo cardItemToAdd = new CardInfo();
        public CardInfo CardItemToAdd
        {
            get { return cardItemToAdd; }
            set
            {
                cardItemToAdd = value;
                RaisePropertyChanged(() => CardItemToAdd);
                RaisePropertyChanged(() => CardAddCommand);
            }
        }
        #endregion

        #region Method
        private void Insert(CardInfo entity)
        {
            Database.CollectionOfCards.InsertOnSubmit(entity);
            Save();
            CardItemToAdd = new CardInfo();
            RaisePropertyChanged(() => CardsCollection);
            RaisePropertyChanged(() => CardAddCommand);
            RaisePropertyChanged(() => CreditCardsCollection);
            RaisePropertyChanged(() => DebitCardsCollection);
        }
        #endregion

        #region Commands
        public ICommand CardAddCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Insert(CardItemToAdd);
                        NavigateBack();
                    }
                };
            }
        }
        #endregion
    }
}
